package com.futvan.z.framework.core;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.StatefulJob;
import org.springframework.web.socket.WebSocketSession;

import java.util.List;

/**
 * 清理已断开连接用户
 */
public class WebSocketClearUserJob implements StatefulJob {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        z.Log("WebSocket在线人数："+z.socket_session_users.size());
        z.Log("在线用户：");
        for (String key : z.socket_session_users.keySet()) {
            List<WebSocketSession> session_list = z.socket_session_users.get(key);
            if(z.isNotNull(session_list) && session_list.size()>0){
                boolean isclose = true;
                //判读所有连接都断开后，执行清理
                for (WebSocketSession session : session_list) {
                    if(session.isOpen()){
                        isclose = false;
                    }else{
                        session_list.remove(session);
                    }
                }
                if(isclose){
                    z.socket_session_users.remove(key);
                    z.session_users.remove(key);
                    z.Log("清理已断开连接用户："+key);
                }else{
                    System.out.print(key+"["+session_list.size()+"] ");
                }
            }

        }
    }
}
